package com.huisou.indexhelp;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

public class DataImport {
	
	public static void oneTimeImportData() {
		String url = "http://localhost:8070/company";
		
		try {
			long start = System.currentTimeMillis();
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet method = new HttpGet(url + "/dataimport?command=full-import&clean=true&commit=true&synchronous=true&offset=0&rows=100");
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String retStr = httpClient.execute(method, responseHandler);
			long end = System.currentTimeMillis();
			System.out.println(retStr);
			System.out.println("=========================" + (end - start) / 1000 + "=====================");
		}
		catch (ClientProtocolException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void intermittentImportData() {
		// 带有SolrCore的基地址
		String baseURL = "http://localhost:8070/news";
		
		// 从哪一条开始
		Integer offset = 0;
		
		// 每次索引多少条
		Integer rows = 5000;
		
		// 总共有多少条
		// Integer total = 2128467;
		// company 1293085
		Integer total = 20000;
		
		// 实体
		String entity = "buy&entity=product";
		
		long start = System.currentTimeMillis();
		while (offset < total) {
			try {
				DefaultHttpClient httpClient = new DefaultHttpClient();
				httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 600000);
				String suffix = buildURL(offset, rows, entity);
				String url = baseURL + suffix;
				System.out.println("===================" + url + "======================");
				HttpGet method = new HttpGet(url);
				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				String retStr = httpClient.execute(method, responseHandler);
				System.out.println(retStr);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			offset += rows;
			
		}
		long end = System.currentTimeMillis();
		System.out.println("=========================" + (end - start) / 1000 + "=====================");
		
	}
	
	public static String buildURL(Integer offset, Integer rows, String entity) {
		return new StringBuffer("/dataimport?command=full-import&clean=false&commit=true&synchronous=true").append("&offset=" + offset).append("&rows=" + rows).toString();
	}
	
	public static void main(String[] args) {
		oneTimeImportData();
		//intermittentImportData();
	}
}
